package com.tricode.ile.training.task.sl275.lab11;

public class CheckingAccount extends Account {

    private double overdraftAmount;

    public CheckingAccount(double initBalance) {
        super(initBalance);
    }

    /**
     * Creates a new instance of Account
     */
    public CheckingAccount(double initBalance, double overDraftAmount) {
        this(initBalance);
        this.overdraftAmount = overDraftAmount;
    }

    public double getOverdraftAmount() {
        return overdraftAmount;
    }

    public void setOverdraftAmount(double overdraftAmount) {
        this.overdraftAmount = overdraftAmount;
    }

    public void withdraw(double amount) throws OverdraftException {
        if (amount > (getBalance() + overdraftAmount)) {
            throw new OverdraftException("The amount is to large", (getBalance() + overdraftAmount) - amount);
        } else {
            double difference = amount - getBalance();
            if (difference > overdraftAmount) {
                throw new OverdraftException("Amount is to large", difference);
            } else {
                setBalance(0.0);
                overdraftAmount -= difference;
            }
        }
    }
}
